<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')"
			   :close-on-click-modal="false">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px"
				 label-position="left">
			<el-form-item label="Data ID" prop="dataId">
				<el-input v-model="form.dataId"></el-input>
			</el-form-item>
			<el-form-item label="配置格式" prop="type">
				<div class="my-2 flex items-center text-sm">
					<el-radio-group v-model="form.type" class="ml-4">
						<el-radio label="yaml" size="small">YAML</el-radio>
						<el-radio label="text" size="small">TEXT</el-radio>
						<el-radio label="json" size="small">JSON</el-radio>
						<el-radio label="html" size="small">HTML</el-radio>
						<el-radio label="xml" size="small">XML</el-radio>
						<el-radio label="properties" size="small">Properties</el-radio>
					</el-radio-group>
				</div>
			</el-form-item>
			<el-form-item label="配置内容" prop="content">
				<textarea v-model="form.content" style="width:100%;height: 300px;
						font-size: 12px;color: #aaa;padding-left:6px;background: #000000;font-family: consolas;line-height: 1.5;word-break:break-all;">
					</textarea>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增配置',
				edit: '编辑配置',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			// 表单数据
			form: {
				type: 'yaml'
			},
			// 验证规则
			rules: {
				name: [
					{required: true, message: '请输入Data ID'}
				],
				type: [
					{required: true, message: '请选择内容格式'}
				],
				content: [
					{required: true, message: '请输入配置内容'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {

		/**
		 * 显示当前组件窗口
		 * @param mode
		 * @returns {default.methods}
		 */
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},

		/**
		 * 表单提交方法
		 */
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == 'add') {
						res = await this.$API.system.params.addNacosInfo.post(this.form);
					} else {
						this.$message.success("暂未开发...")
					}
					this.isSaveing = false;
					if (res.status == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					}
				}
			})
		},

		/**
		 * 表单注入数据
		 * @param data
		 */
		setData(data) {
			Object.assign(this.form, data)
		}
	}
}
</script>

<style>
</style>
